/*
Kiwi Editor is a framework for building WYSIWYG web editors.
Copyright (C) 2011  Roosevelt de Lima Sardinha

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

Contact the author at roosevelt1@gmail.com (Roosevelt) 
*/

function jstext()
{
	// Just for not confusing.
	var thisObject = this;

	this.useTag = function(textarea, text){
		// Encapsulates the selected text with the passed tag.
		if (text.indexOf(' ') != -1)
		{
			var tag = text.substring(0,text.indexOf(' '));
			var attributes = ' ' + text.substring(text.indexOf(' '));
		}
		else
		{
			var tag = text;			
			var attributes = '';
		}
		if(document.selection)  // If we're using IE.
		{
			// Builds the new text
			document.selection.createRange().text = '<' + tag + attributes +  '>' + document.selection.createRange().text + '</' + tag + '>';
		}
		else if(typeof(textarea.selectionStart) != "undefined")  // If we're using a Netscape based browser.
		{
			var before, selected, after;
			before = textarea.value.substring(0, textarea.selectionStart);
			selected = textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
			after = textarea.value.substring(textarea.selectionEnd);

			//Builds the new text
			textarea.value = before + '<' + tag + attributes + '>' + selected + '</' + tag + '>' + after;
		}
	}
	
        this.selectionBold = function(textarea) {
		// Makes selection bold.
		this.useTag(textarea, 'b');
	}

	this.selectionItalic = function(textarea)
	{
		// Makes selection italic.
		this.useTag(textarea, 'i');
	}
	
	this.selectionUnderline = function(textarea)
	{
		// Makes selected text underlined.
		this.useTag(textarea, 'u');
	}

	this.selectionAlign = function(textarea, alignment)
	{
		// Changes  alignment of the selected text;
		switch(alignment)
		{
			case 'left':
				this.useTag(textarea, "p align='left'");
				break;
			case 'center':
				this.useTag(textarea, "p align='center'");
				break;
			case 'right':
				this.useTag(textarea, "p align='right'");
				break;
			case 'justify':
				this.useTag(textarea, "p align='justify'");
				break;
			default:
				break;
		}
	} 
}
